﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using IndexMarkSilverlightClient.Views;
using IndexMarkSilverlightClient.Classes;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace IndexMarkSilverlightClient
{
    public partial class ErrorWindow : ChildWindow
    {
        
        public ErrorWindow(Exception e)
        {
            InitializeComponent();
            ErrorTextBox.Text = e.Message;

            TreeViewItem root = new TreeViewItem();
            StackPanel panel = new StackPanel();
            panel.Background = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));
            panel.Orientation = Orientation.Horizontal;
            StackPanel ChildPanel = new StackPanel();
            ChildPanel.Orientation = Orientation.Vertical;
            TextBlock Message = new TextBlock();
            Message.Text = e.Message;
            TextBlock StackTrace = new TextBlock();
            StackTrace.FontSize = 10;
            StackTrace.Text = e.StackTrace;
            BitmapImage image = new BitmapImage(new Uri("Resources/Error.png", UriKind.Relative));
            Image icon = new Image();
            icon.Source = image;
            panel.Children.Add(icon);
            ChildPanel.Children.Add(Message);
            ChildPanel.Children.Add(new TextBlock(){ Width=10 });
            ChildPanel.Children.Add(StackTrace);
            panel.Children.Add(ChildPanel);

            root.Header = panel;
            ErrorTreeView.Items.Add(root);

            Exception inner = e.InnerException;
            while (inner != null)
            {
                TreeViewItem item = new TreeViewItem();
                StackPanel panel2 = new StackPanel();
                panel2.Orientation = Orientation.Horizontal;
                panel2.Background = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));
                StackPanel ChildPanel2 = new StackPanel();
                ChildPanel2.Orientation = Orientation.Vertical;
                TextBlock Message2 = new TextBlock();
                Message2.Text = inner.Message;
                TextBlock StackTrace2 = new TextBlock();
                StackTrace2.FontSize = 10;
                StackTrace2.Text = inner.StackTrace;
                BitmapImage image2 = new BitmapImage(new Uri("Resources/Error.png", UriKind.Relative));
                Image icon2 = new Image();
                icon2.Source = image2;
                panel2.Children.Add(icon2);
                ChildPanel2.Children.Add(Message2);
                ChildPanel2.Children.Add(new TextBlock() { Width=10 });
                ChildPanel2.Children.Add(StackTrace2);
                panel2.Children.Add(ChildPanel2);

                item.Header = panel2;
                root.Items.Add(item);
                inner = inner.InnerException;
                root = item;
            }
        }

        public ErrorWindow(Uri uri)
        {
            InitializeComponent();
            if (uri != null)
            {
                ErrorTextBox.Text = "Страница не найдена: \"" + uri.ToString() + "\"";
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}